<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/7/1 9:36
 * Modified Time: 2019/7/1 9:36
 * Version: 2.0
 */

namespace App\Modules\MemberCard\Repositores;


use App\Exceptions\NotFoundException;
use App\Models\CardType;
use App\Modules\MemberCard\Validators\CardTypeValidator;
use Flower\Core\Repository\Eloquent\BaseRepository;

class CardTypeRepository extends BaseRepository
{
    public function model()
    {
        return CardType::class;
    }

    public function validator()
    {
        return CardTypeValidator::class;
    }

    /**
     * 新增会员卡类
     * @param $store_id
     * @param $category
     * @param $price
     * @param $name
     * @param $present
     * @param $count
     * @param $royalty
     * @return mixed
     */
    public function createCardType($store_id, $category, $price, $name, $present, $count, $royalty,$discount_way,$skin,$unit_price,$is_general,$assist_royalty)
    {
        $data = [
            'store_id' => $store_id,
            'category' => $category,
            'price' => $price,
            'name' => $name,
            'royalty' => $royalty,
            'discount_way' => $discount_way,
            'skin' => $skin,
            'unit_price' => $unit_price,
            'is_general' => $is_general,
            'assist_royalty' => $assist_royalty
        ];

        if ($category == 1) {
            $data['count'] = $count;
        } else {
            $data['present'] = $present;
        }
        return $this->create($data);
    }

    /**
     * 编辑会员卡类
     * @param $store_id
     * @param $category
     * @param $price
     * @param $name
     * @param $present
     * @param $count
     * @param $royalty
     * @return mixed
     */
    public function editCardType($card_type_id, $category, $price, $name, $present, $count, $royalty,$discount_way,$skin,$unit_price,$is_general,$assist_royalty)
    {
        $data = [];
        if($category!==null){
            $data['category'] = $category;
        }
        if($price!==null){
            $data['price'] = $price;
        }
        if($name!==null){
            $data['name'] = $name;
        }
        if($royalty!==null){
            $data['royalty'] = $royalty;
        }

	    if($assist_royalty!==null){
		    $data['assist_royalty'] = $assist_royalty;
	    }

        if($discount_way!==null){
            $data['discount_way'] = $discount_way;
        }
        if($skin!==null){
            $data['skin'] = $skin;
        }
        if($unit_price!==null){
            $data['unit_price'] = $unit_price;
        }
        if($is_general!==null){
            $data['is_general'] = $is_general;
        }

        if ($category == 1) {
            if($count){
                $data['count'] = $count;
            }
        } else {
            if($present){
                $data['present'] = $present;
            }
        }
        return $this->update($data,$card_type_id);
    }

    /**
     * 获取办理会员数
     * @param $card_type_id
     * @return mixed
     */
    public function getMemberCardTypeCount($card_type_id){
        return $this->model->find($card_type_id)->withCount('memberCard')->first();
    }

    /**
     * 开关会员卡类
     * @param $card_type_id
     * @param $status
     * @return mixed
     */
    public function switchCardType($card_type_id,$status)
    {
        $data = [
            'status'=>$status
        ];

        return $this->update($data,$card_type_id);
    }

    /**
     * 店铺会员卡类状态统计
     * @param $store_id
     * @param $status
     * @return mixed
     */
    public function getStatusMemberCardNum($store_id, $status)
    {
        return $this->model
            ->where('status', $status)
            ->where('store_id', $store_id)
            ->get()
            ->count();
    }

    public function isExisted($id){
    	$info = $this->find($id);
    	if(empty($info)){
    		throw  new NotFoundException(['msg'=>"会员卡类型不存在"]);
	    }
    	return $info;
    }

}